from fastapi import APIRouter, Depends, status
from app.api.deps import CurrentUser, SessionDep
from app.services.flight_service import FlightService
from app.models.flight import FlightPriceQueryCondition,PolicysFillFocus,FlightList,CarrierListResponse, DepartureListResponse, DestinationListResponse, FlightQueryCondition, FlightQueryResponse, SimpleFlightQueryResponse, FlightQueryPaginatedResponse

router = APIRouter()


@router.get("/carriers", response_model=CarrierListResponse)
async def get_carriers(
    session: SessionDep, current_user: CurrentUser
) -> CarrierListResponse:
    """
    获取所有航空公司的名称列表
    
    Returns:
        航空公司名称列表
    """
    # 创建航班服务实例
    service = FlightService(db=session, current_user=current_user)
    
    # 获取航空公司列表
    carriers = await service.get_carriers()
    
    # 返回响应
    if len(carriers) == 0:
        return CarrierListResponse(
            code=status.HTTP_404_NOT_FOUND,
            message="没有找到航空公司",
            data=None
        )
    
    return CarrierListResponse(
        code=0,
        message="查询成功",
        data=carriers
    )


@router.get("/departures", response_model=DepartureListResponse)
async def get_departures(
    session: SessionDep, current_user: CurrentUser
) -> DepartureListResponse:
    """
    获取所有出发地列表
    
    Returns:
        出发地列表
    """
    # 创建航班服务实例
    service = FlightService(db=session, current_user=current_user)
    
    # 获取出发地列表
    departures = await service.get_departures()
    
    # 返回响应
    if len(departures) == 0:
        return DepartureListResponse(
            code=status.HTTP_404_NOT_FOUND,
            message="没有找到出发地",
            data=None
        )
    
    return DepartureListResponse(
        code=0,
        message="查询成功",
        data=departures
    )


@router.get("/destinations", response_model=DestinationListResponse)
async def get_destinations(
    session: SessionDep, current_user: CurrentUser
) -> DestinationListResponse:
    """
    获取所有目的地列表
    
    Returns:
        目的地列表
    """
    # 创建航班服务实例
    service = FlightService(db=session, current_user=current_user)
    
    # 获取目的地列表
    destinations = await service.get_destinations()
    
    # 返回响应
    if len(destinations) == 0:
        return DestinationListResponse(
            code=status.HTTP_404_NOT_FOUND,
            message="没有找到目的地",
            data=None
        )
    
    return DestinationListResponse(
        code=0,
        message="查询成功",
        data=destinations
    )


@router.post("/flight", response_model=FlightQueryPaginatedResponse)
async def query_flights(
    session: SessionDep, 
    current_user: CurrentUser,
    condition: FlightQueryCondition
) -> FlightQueryPaginatedResponse:
    """
    根据条件查询航班信息（使用 t_flight_list 表）
    现在支持分页查询，返回分页信息
    
    Args:
        condition: 查询条件，包括出发地、到达地、出发时间范围、航班号等
        
    Returns:
        符合条件的航班信息列表（包含分页信息）
    """
    # 创建航班服务实例
    service = FlightService(db=session, current_user=current_user)
    
    # 查询航班信息
    flights = await service.query_simple_flights(condition)
    return flights


@router.get("/flight-price/{id}", response_model=FlightQueryResponse)
async def query_flight_by_id(
    id: str,
    session: SessionDep, 
    current_user: CurrentUser
) -> FlightQueryResponse:
    """
    根据ID查询航班信息（使用 t_policys_fill_focus 表）
    
    Args:
        id: 航班记录ID
        
    Returns:
        符合ID的航班信息
    """
    # 创建航班服务实例
    service = FlightService(db=session, current_user=current_user)
    
    # 根据ID查询航班信息
    flight = await service.query_flights_price_by_id(id)
    return flight
